package com.fxy.demowebsockettest.controller;

import com.fxy.demowebsockettest.pojo.TbUser;
import com.fxy.demowebsockettest.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 范欣宜
 * @Date 2020-12-1
 **/
@Controller
@Slf4j
@Api(tags = "接口-客服管理")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * @Description:查看客服人员列表
     * @Author: 范欣宜
     * @Date: 2020/12/1
     * @return: java.lang.String
     **/
    @GetMapping("/list")
    @ApiOperation(value = "查看客服人员列表", notes = "查看客服人员列表")
    public String getUserList(Model model) {

        List<TbUser> list=userService.list();
        model.addAttribute("list", list);

        return "/list";
    }

    /**
     * @Description:删除客服人员
     * @Author: 范欣宜
     * @Date: 2020/12/1 1
     * @param id:
     * @return: java.lang.String
     **/
    @GetMapping("/deleteUser")
    @ApiOperation(value = "删除客服人员", notes = "删除客服人员")
    public ModelAndView getUserList(@RequestParam("id") Integer id) {
        userService.delete(id);
        return new ModelAndView("redirect:/list");
    }

    /**
     * @Description:根据id修改
     * @Author: 范欣宜
     * @Date: 2020/12/1
     * @param id:
     * @return: java.lang.String
     **/
    @GetMapping("/toUpdateUser/{id}")
    @ApiOperation(value = "删除客服人员", notes = "删除客服人员")
    public String toUpdateUser(@PathVariable("id") Integer id) {
        return "update";
    }

    /**
     * @Description:回显客服人员信息
     * @Author: 范欣宜
     * @Date: 2020/12/1
     * @param id:
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    @ResponseBody
    @ApiOperation(value = "回显客服人员信息", notes = "回显客服人员信息")
    @PostMapping("/getUpdateData")
    public Map<String, Object> getUpdateData(Integer id){
        HashMap<String,Object> map = new HashMap<String,Object>(10);
        //对象的回显--------根据id获取用户信息
        TbUser user=userService.getById(id);
        //将查询的所有数据存入map集合，并返回到前台
        map.put("user", user);
        return map;
    }

    /**
     * @Description:修改客服人员
     * @Author: 范欣宜
     * @Date: 2020/12/1
     * @param user:
     * @return: org.springframework.web.servlet.ModelAndView
     **/
    @ApiOperation(value = "修改客服人员", notes = "修改客服人员")
    @PostMapping("/updateUser")
    public ModelAndView updateUser(TbUser user) {
        userService.update(user);
        return new ModelAndView("redirect:/list");
    }

    /**
     * @Description:添加跳转
     * @Author: 范欣宜
     * @Date: 2020/12/1
     * @return: java.lang.String
     **/
    @GetMapping("/toAddUser")
    @ApiOperation(value = "添加跳转", notes = "添加跳转")
    public String toAddUser() {
        return "add";
    }

    /**
     * @Description:添加客服人员
     * @Author: 范欣宜
     * @Date: 2020/12/1
     * @param user:
     * @return: org.springframework.web.servlet.ModelAndView
     **/
    @PostMapping("/addUser")
    @ApiOperation(value = "添加客服人员", notes = "添加客服人员")
    public ModelAndView addUser(TbUser user) {
        userService.add(user);
        return new ModelAndView("redirect:/list");
    }
}
